package com.imooc.bilibili.service;

import com.imooc.bilibili.domain.auth.*;
import com.imooc.bilibili.domain.constant.AuthRoleConstant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * ProjectName imooc-bilibili
 *
 * @author xieyucan
 * <br>CreateDate 2022/10/24 18:39
 */
@Service
public class UserAuthService {

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private AuthRoleService authRoleService;

    public UserAuthorities getUserAuthorities(Long userId) {
        List<UserRole> userRoleList=userRoleService.getUserRoleByUserId(userId);
        Set<Long> roleSet=userRoleList.stream().map(UserRole::getRoleId).collect(Collectors.toSet());
        List<AuthRoleElementOperation> roleElementOperationList=authRoleService.getRoleElementOperationList(roleSet);
        List<AuthRoleMenu> authRoleMenuList=authRoleService.getAuthRoleMenuByRoleId(roleSet);
        UserAuthorities userAuthorities = new UserAuthorities();
        userAuthorities.setRoleElementOperationList(roleElementOperationList);
        userAuthorities.setRoleMenuList(authRoleMenuList);
        return userAuthorities;
    }

    public void addUserDefaultRole(Long id) {
        UserRole userRole = new UserRole();
        AuthRole authRole=authRoleService.getRoleByCode(AuthRoleConstant.ROLE_CODE_LV0);
        userRole.setRoleId(authRole.getId());
        userRole.setUserId(id);
        userRoleService.addUserRole(userRole);
    }
}
